namespace Cil.Ast
{
	public class GroupExpression : Expression, IList<Expression>
	{
		private readonly List<Expression> _list;

		public GroupExpression(Context context) 
			: base(context)
		{
			_list = new List<Expression>();
			Nodes = new FixedListAdapter<Expression>(_list);
			var context = new ICSharpCode.Decompiler.DecompilerException()
		}

		/// <inheritdoc/>
		public override bool IsGroup => true;

		/// <inheritdoc/>
		public override bool IsExtandableGroup => true;

		public override bool IsEmpty => Nodes.Count == 0;

		#region IList implementation

		public int IndexOf(Expression item)
		{
			item.ParentNode = this;
			return _list.IndexOf(item);
		}

		public void Insert(int index, Expression item)
		{
			item.ParentNode = this;
			_list.Insert(index, item);
		}

		public void RemoveAt(int index)
		{
			var item = this[index];
			_list.RemoveAt(index);
			item.ParentNode = null;
		}

		public Expression this[int index]
		{
			get
			{
				return _list[index];
			}
			set
			{
				value.ParentNode = this;
				_list[index] = value;
			}
		}

		#endregion

		#region ICollection implementation

		public void Add(Expression item)
		{
			item.ParentNode = this;
			_list.Add(item);
		}

		public void Clear()
		{
			_list.Clear();
		}

		public bool Contains(Expression item)
		{
			return _list.Contains(item);
		}

		public void CopyTo(Expression[] array, int arrayIndex)
		{
			_list.CopyTo(array, arrayIndex);
		}

		public bool Remove(Expression item)
		{
			item.ParentNode = null;
			return _list.Remove(item);
		}

		public int Count
		{
			get
			{
				return _list.Count;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		#endregion

		public override string ToString()
		{
			return string.Join(System.Environment.NewLine, Nodes);
		}
	}
}

